﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using EHRForce.Models;
using EHRForce.Helpers;

namespace EHRForce.Controllers
{
    public class PatientsController : Controller
    {
        PatientRepository patientRepository = new PatientRepository();

        //
        // GET: /Patients/
        //      /Patients/Page/2

        public ActionResult Index(int? page)
        {
            const int pageSize = 25;

            var patients = patientRepository.FindAllPatients();
            var paginatedPatients = new PaginatedList<Patient>(patients,
                                                               page ?? 0,
                                                               pageSize);

            return View(paginatedPatients);
        }

        //
        // GET: /Patients/Details/2

        public ActionResult Details(int id)
        {
            Patient patient = patientRepository.GetPatient(id);

            if (patient == null)
                return View("NotFound");
            else
                return View(patient);
        }

        //
        // GET: /Patients/Edit/2

        public ActionResult Edit(int id)
        {
            Patient patient = patientRepository.GetPatient(id);

            return View(new PatientFormViewModel(patient));
        }

        //
        // POST: /Dinners/Edit/2

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(int id, FormCollection formValues)
        {
            Patient patient = patientRepository.GetPatient(id);
            try
            {
                UpdateModel(patient);
                patientRepository.Save();
                return RedirectToAction("Details", new { id = patient.PatientUID });
            }
            catch
            {
                ModelState.AddRuleViolations(patient.GetRuleViolations());
                return View(new PatientFormViewModel(patient));
            }
        }

        //
        // GET: /Patients/Create

        [Authorize]
        public ActionResult Create()
        {
            Patient patient = new Patient()
            {
                LastName = "Unknown"
            };
            return View(new PatientFormViewModel(patient));
        }

        //
        // POST: /Patients/Create

        [AcceptVerbs(HttpVerbs.Post), Authorize]
        public ActionResult Create(Patient patient)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    patient.CreatedByUser = User.Identity.Name;

                    PtAccount ptAccount = new PtAccount();
                    ptAccount.CreatedByUser = User.Identity.Name;
                    patient.PtAccounts.Add(ptAccount);

                    patientRepository.Add(patient);
                    patientRepository.Save();

                    return RedirectToAction("Details", new { id = patient.PatientUID });
                }
                catch
                {
                    ModelState.AddRuleViolations(patient.GetRuleViolations());
                }
            }

            return View(new PatientFormViewModel(patient));
        }

        //
        // GET: /Patients/Delete/1

        public ActionResult Delete(int id)
        {
            Patient patient = patientRepository.GetPatient(id);

            if (patient == null)
                return View("NotFound");
            else
                return View(patient);
        }

        //
        // POST: /Dinners/Delete/1
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Delete(int id, string confirmButton)
        {
            Patient patient = patientRepository.GetPatient(id);

            if (patient == null)
                return View("NotFound");

            patientRepository.Delete(patient);
            patientRepository.Save();

            return View("Deleted");
        }
    }
}
